package com.hfy.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfy.model.dto.user.OpenSessionDto;
import com.hfy.model.po.user.UserCommentPo;
import com.hfy.model.po.user.SessionPo;
import com.hfy.model.vo.session.SessionVo;
import com.hfy.user.mapper.SessionMapper;
import com.hfy.user.service.InfoService;
import com.hfy.user.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;
import utils.CheckUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ author 韩
 * time: 2024/11/4 9:32
 */

@Service
public class SessionServiceImpl extends ServiceImpl<SessionMapper, SessionPo> implements SessionService {

    @Autowired
    private InfoService infoService;
    @Override
    public Result open(OpenSessionDto dto) {
        // 每一个会话都是通过两个账号唯一决定的，所以根据这两个账号生成的id也是唯一的，由于会话有一些自己的属性
        // 所以不能把它耦合在一个表中

        LambdaQueryWrapper<SessionPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SessionPo::getAccount, dto.getMyAccount())
                .eq(SessionPo::getToAccount, dto.getOtherAccount());
        SessionPo one = getOne(queryWrapper);
        if (one != null) {
            // 2. 如果记录存在，则更新 is_die 字段为 0
            one.setIsDie(0);
            updateById(one);
        } else if (dto.getOtherAccount() != null) {
            // 3. 如果记录不存在，则新增记录
            SessionPo newSession = new SessionPo();
            newSession.setAccount(dto.getMyAccount());
            newSession.setToAccount(dto.getOtherAccount());
            newSession.setRecentTime(LocalDateTime.now());
            newSession.setIsDie(0);
            save(newSession);
        }
        SessionPo result = getOne(queryWrapper);
        return Result.ok(result);
    }

    @Override
    public Result getSessionList(String account) {
        LambdaQueryWrapper<SessionPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SessionPo::getId, SessionPo::getToAccount, SessionPo::getSimpleMessage, SessionPo::getRecentTime, SessionPo::getNoticeNum)
                .eq(SessionPo::getAccount, account).eq(SessionPo::getIsDie, 0).orderBy(true, false, SessionPo::getRecentTime);

        List<SessionPo> list = list(queryWrapper);
        List<SessionVo> vos = new ArrayList<>();
        list.forEach((item) -> {
            SessionVo vo = BeanUtil.copyProperties(item, SessionVo.class, /*"account",*/ "isDie");
            UserCommentPo info = infoService.getInfoByAccount(Long.valueOf(vo.getToAccount()));
            vo.setToAvatar(info.getAvatar());
            vo.setToName(info.getUsername());
            vo.setRecords(new ArrayList<>());
            vos.add(vo);
        });
        return Result.ok(vos);
    }

    @Override
    public Result getSession(String account, String toAccount) {
        LambdaQueryWrapper<SessionPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SessionPo::getId, SessionPo::getToAccount, SessionPo::getSimpleMessage, SessionPo::getRecentTime, SessionPo::getNoticeNum)
                .eq(SessionPo::getAccount, account).eq(SessionPo::getToAccount, toAccount).eq(SessionPo::getIsDie, 0);
        SessionPo sessionPo = getOne(queryWrapper);

        SessionVo vo = BeanUtil.copyProperties(sessionPo, SessionVo.class);
        UserCommentPo info = infoService.getInfoByAccount(Long.valueOf(vo.getToAccount()));
        vo.setToAvatar(info.getAvatar());
        vo.setToName(info.getUsername());
        vo.setRecords(new ArrayList<>());

        return Result.ok(vo);
    }

    @Override
    public Result removeSession(String account, String toAccount) {
        // 1. 先校验参数
        if (!CheckUtils.theStringIsLong(account) || !CheckUtils.theStringIsLong(toAccount)) {
            return Result.error("账号必须是一串数字");
        }

        // 2.然后直接进行业务操作，进行会话的逻辑删除
        LambdaUpdateWrapper<SessionPo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SessionPo::getIsDie, 1).eq(SessionPo::getAccount, account).eq(SessionPo::getToAccount, toAccount);
        update(updateWrapper);

        return Result.ok();
    }


}
